//
// Created by 32827 on 2024/2/1.
//
#include "widget/yzl_ui_widget_pp_button.hpp"
#include "yzl.h"
WidgetButton::~WidgetButton()= default;

WidgetButton::WidgetButton(YZL_WIDGET *Ram, int x,int y, int z, int width, int height, utf8 *text) : WidgetBase(Ram, x, y, z, width, height) {
    text_color = WHITE;
    up_color = BLACK;
    down_color = RED;
    listener = default_listener;

    yzl_ui_font_data_init(&fontPixel,&this->text,&textLen,text);

    uint32_t size= yzl_ui_get_string_width(fontPixel,this->text, textLen);  //调整控件大小
    uint16_t nWidth=*((uint16_t *)(&size)+1);
    uint16_t nHeight=*(uint16_t *)(&size);
    if(width-2<nWidth){
        width=nWidth+2;
    }
    if(height-2<nHeight){
        height=nHeight+2;
    }
    instance->width=width;
    instance->height=height;
}
WidgetButton::WidgetButton(YZL_WIDGET *Ram, UI_Rect *rect, int z, utf8 *text) : WidgetButton(Ram, rect->x, rect->y, z, rect->width, rect->height,text){

}
void WidgetButton::default_listener(void *widget, YZL_UI_CTR_EVENT event) {
    if(event==YZL_UI_CTR_EVENT_TOUCH_UP){
        yz_info("C++ default_listener\n");
    }
}

void WidgetButton::drawP() {
    WidgetBase::drawP();
    if(instance->status==YZL_UI_CTR_EVENT_TOUCH_UP){
        uint32_t size= yzl_ui_get_string_width(fontPixel,text, textLen);
        uint16_t width=(size>>16);
        uint16_t height=(size&0xffff);
        yzl_ui_draw_rect(instance->x,instance->y,instance->width,instance->height,YZF_TRUE,up_color);
        yzl_ui_draw_string(instance->x+(instance->width-width)/2,instance->y+(instance->height-height)/2,instance->x+instance->width,
                           text,textLen,WHITE,text_color);
    }else if(instance->status==YZL_UI_CTR_EVENT_TOUCH_DOWN){
        uint32_t size= yzl_ui_get_string_width(fontPixel,text, textLen);
        uint16_t width=(size>>16);
        uint16_t height=(size&0xffff);
        yzl_ui_draw_rect(instance->x,instance->y,instance->width,instance->height,YZF_TRUE,down_color);
        yzl_ui_draw_string(instance->x+(instance->width-width)/2,instance->y+(instance->height-height)/2,instance->x+instance->width,
                           text,textLen,WHITE,text_color);
    }
    instance->isRender=YZF_FALSE;
}

YZF_BOOL WidgetButton::touchP(uint16_t x, uint16_t y, YZL_UI_CTR_EVENT event) {
    WidgetBase::touchP(x, y, event);
    if(instance->status==YZL_UI_CTR_EVENT_TOUCH_UP&& event == YZL_UI_CTR_EVENT_TOUCH_DOWN){
        instance->status=YZL_UI_CTR_EVENT_TOUCH_DOWN;
        instance->isRender=YZF_TRUE;
        listener(instance,YZL_UI_CTR_EVENT_TOUCH_DOWN);
        return YZF_TRUE;
    }else if(instance->status==YZL_UI_CTR_EVENT_TOUCH_DOWN&& event == YZL_UI_CTR_EVENT_TOUCH_UP){
        instance->status=YZL_UI_CTR_EVENT_TOUCH_UP;
        instance->isRender=YZF_TRUE;
        if(yzl_ui_point_check(instance,x,y)){
            listener(instance,YZL_UI_CTR_EVENT_TOUCH_UP);
            return YZF_TRUE;
        }else{
            listener(instance,YZL_UI_CTR_EVENT_TOUCH_UP);
            return YZF_FALSE;
        }
    }
    return YZF_FALSE;
}

void WidgetButton::disposeP() {
    YZL_MEM_MANAGER.pFree(text);
    YZL_MEM_MANAGER.pFree(fontPixel);
    WidgetBase::disposeP();
}

void WidgetButton::setListener(void (*listener)(void *, YZL_UI_CTR_EVENT)) {
    this->listener=listener;
}